{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.0 64-bit ('venv')",
   "metadata": {
    "interpreter": {
     "hash": "1ee744fdee3946c2af2bdc58f3796819357ed068616caa870dc20aacb53d0af6"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建模型和字段的典型方式\n",
    "\n",
    "import datetime\n",
    "from peewee import *\n",
    "from playhouse.sqlite_ext import *\n",
    "\n",
    "db = SqliteDatabase('my_app.db')\n",
    "\n",
    "class BaseModel(Model):\n",
    "    class Meta:\n",
    "        database = db\n",
    "\n",
    "class User(BaseModel):\n",
    "    username = CharField(unique = True)\n",
    "\n",
    "class Tweet(BaseModel):\n",
    "    user = ForeignKeyField(User, backref='tweets')\n",
    "    message = TextField()\n",
    "    created_date = DateTimeField(default = datetime.datetime.now)\n",
    "    is_published = BooleanField(default=True)\n",
    "  \n",
    "class Message(BaseModel):\n",
    "    user = ForeignKeyField(User, backref='messages')\n",
    "    read_count = IntegerField(default=0)\n",
    "    read_count_ = BigIntegerField()\n",
    "    info = JSONField() # 在playhouse里面导入\n",
    "    timestamp = DateTimeField(default=datetime.datetime.now)\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "db.create_tables([User, Tweet, Message])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "SyntaxError",
     "evalue": "invalid character '：' (U+FF1A) (<ipython-input-14-42556eac347c>, line 6)",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-14-42556eac347c>\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m    for tweet in tweets：\u001b[0m\n\u001b[1;37m                       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character '：' (U+FF1A)\n"
     ]
    }
   ],
   "source": [
    "tweets = (Tweet\n",
    "          .select(Tweet, User).\n",
    "          join(User).o\n",
    "          rder_by(Tweet.created_date).dsc())\n",
    "      for tweet in tweets：\n",
    "    print()tweet.user.username, tweet.message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}